package org.grill.fatwhacker;

import java.io.File;

import org.eclipse.swt.widgets.Display;
import org.grill.fatwhacker.data.WeekSet;
import org.grill.fatwhacker.gui.MainWindow;
import org.joda.time.LocalDate;

public class FatWhacker {
	static Display d;
	public static String version = "0.0.4";
	
	public static int weekStart = 0;
	
	WeekSet weekSet;
	
	private void init() {
		weekSet = WeekSet.restore();
		if (weekSet == null) {
			weekSet = new WeekSet();
			weekSet.initialize();
		} else {
			weekSet.getFoodWeek(new LocalDate().plusWeeks(1));
		}
	}
	
	public static String getDataFolder() {
		String setLoc = System.getProperty("fatwhacker.datafolder");
		if (setLoc != null && setLoc.length() != 0) {
			new File(setLoc).mkdirs();
			return setLoc;
		}
		
		if ("Mac OS X".equals(System.getProperty("os.name"))) {
			File file = new File(System.getProperty("user.home") + "/Library/Application Support/FatWhacker");
//			File file = new File("/Volumes/vault/workspace/");
			file.mkdirs();
			return file.getAbsolutePath();
		}
		else return ".";
	}

	public FatWhacker() {
		Display.setAppName("FatWhacker");
		d = new Display();
		init();
		
		MainWindow w = new MainWindow(d);
		w.setWeekSet(weekSet);
		w.open();
		
		d.syncExec(new Runnable() {
		
			public void run() {
				while (!d.isDisposed() && d.getShells().length > 0)
					if (!d.readAndDispatch())
						d.sleep();
			}
		});
	}
	
	public static void main(String[] args) {
		new FatWhacker();
	}
}
